package com.lz.es;

import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;

import java.io.IOException;

/**
 * @description
 *
 * @date 2021-11-25 21:09
 *
 * @author Lizhong
 */
public class EsSql {


    public void query() throws IOException {


        RestClient restClient = RestClient.builder(
                new HttpHost("test.com", 9200, "http")
        ).build();

        String method = "GET";
        String endPoint = "/_sql";
        Request request = new Request(method, endPoint);
        request.addParameter("format", "json");
        request.setJsonEntity("{\"query\":\"select * from iot  order by  time desc LIMIT 5 \"}");

        Response response = restClient.performRequest(request);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    public static EsSql getInstance() {
        return new EsSql();
    }

    public static void main(String[] args) throws IOException {
        EsSql.getInstance().query();
    }
}
